package com.panopset.db;

import java.io.StringWriter;

/**
 *
 * @author Karl Dinwiddie
 *
 */
public class FieldIdFK extends DataField {

    private final DataField fk;
    private final TableDescription<?> fkt;

    public FieldIdFK(TableDescription<?> fkTable, DataField fkField) {
        super(fkField.getName());
        setIndexed(true);
        fk = fkField;
        fkt = fkTable;
    }

    @Override
    public String getAttributes() {
        return fk.getAttributes();
    }

    private String fkAttrs;

    public String getAdditionalAttributes() {
        if (fkAttrs == null) {
            StringWriter sw = new StringWriter();
            sw.append(" foreign key(");
            sw.append(fk.getName());
            sw.append(") references ");
            sw.append(fkt.getFullTableName());
            sw.append("(id)");
            fkAttrs = sw.toString();
        }
        return fkAttrs;
    }

    public TableDescription<?> getBoundTable() {
        return fkt;
    }
}
